package com.bdqn.springweb;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.springweb.mapper.UserMapper;
import com.bdqn.springweb.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
public class SysUserTester {

    @Resource
    UserMapper UserMapper;

    /**
     * 根据id查询用户
     */
    @Test
    public void testSelectById() {
        User sysUser = UserMapper.selectById(1L);
        System.out.println("usrName------->" + sysUser.getUsrName());
    }

    /**
     * 添加
     */
    @Test
    public void testInsert() {
        try {
            UserMapper.insert(new User("Mybatis-Plus", "123456", 9L, 1));
            System.out.println("新增成功");
        } catch (Exception e) {
            System.out.println("新增失败，原因---》" + e.getMessage());
        }
    }

    /**
     * 查询所有
     */
    @Test
    public void testFindAll() {
        List<User> Users = UserMapper.selectList(null);
        for (User User : Users) {
            System.out.println("userName------>" + User.getUsrName());
        }
    }

    /**
     * 条件查询：根据用户名和密码进行查询
     */
    @Test
    public void testFind() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_name", "MyBatis-Plus");
        wrapper.eq("usr_password", "123456");
        List<User> Users = UserMapper.selectList(wrapper);
        if (Users != null) {
            for (User User : Users) {
                System.out.println("usrName：" + User.getUsrName());
            }
        }
    }

    @Test
    public void testPagination() {// 分页查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_role_id", 8L);
        //分页对象：查询获得 User 对象，查看第 1 页数据，每页显示 2 条记录
        Page<User> page = new Page<User>(1, 2);
        IPage<User> userPager = UserMapper.selectPage(page, wrapper);
        System.out.println("总记录数 ---- --> " + userPager.getTotal());
        System.out.println("总页数 ------ --> " + userPager.getPages());
        System.out.println("当前页数 ----- -> " + userPager.getCurrent());
        System.out.println("每页记录数 -----> " + userPager.getSize());
        System.out.println("当前记录： -----> ");
        for (User user : userPager.getRecords()) {//获得查询记录
            System.out.println("usrId：" + user.getUsrId());
        }
    }
}
